﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using LIU.Tangtu.Web.Models;
using LIU.Tangtu.Web.App_Code;
using LIU.Tangtu.IServices.Sys;
using Microsoft.AspNetCore.Authorization;
using LIU.Framework.Core;
using System.Reflection;
using LIU.Framework.WebApi;

namespace LIU.Tangtu.Web.Controllers
{

    [Route("Api/Home")]
    public class HomeController : AuthBaseController
    {
        private readonly LIU.Framework.Core.Log.ILogger _logger = AppInstance.Current.GetLogger("测试");
        private readonly IUserInfoService userInfoService;
        public HomeController()
        {

            userInfoService = ServiceBus.Get<IUserInfoService>();
        }

        [Route("GetUser", Name = "获取用户信息"),HttpPost]
        public async Task<Result> GetUser()
        {
            //_logger.LogInformation("信息信息信息信息信息信息信息信息");
            //_logger.LogWarning("警告警告警告警告警告警告警告警告警告");
            //_logger.LogDebug("调试调试调试调试调试调试调试调试调试");
            //_logger.LogError("错误错误错误错误错误错误错误错误错误");
            return await Result.OKAsync(CurrentUser);
            // return await Result.OKAsync(userInfoService.GetUsers(p => true));
        }



        [Route("test2/{msg}/tt"),HttpGet]
        [AllowAnonymous]
        public async Task<Result> Test2(string msg)
        {
            return await Result.OKAsync(msg);
        }

        /// <summary>
        /// 获取的api
        /// </summary>
        /// <param name="isAuth">True需要验证的api</param>
        /// <returns></returns>
        [Route("AllApi/{isAuth=0}", Name = "获取所有的api"), HttpGet]
        public async Task<Result> AllApi(int isAuth)
        {
            var type = isAuth == 1 ? typeof(AuthBaseController) : typeof(BaseController);
            var types = AppInstance.Current.Finder.FindTypes(p => type.IsAssignableFrom(p) && p.GetCustomAttribute<RouteAttribute>() != null);
            List<object> list = new List<object>();
            foreach (var item in types)
            {
                string controllerName = item.Name.Replace("Controllers", "", StringComparison.CurrentCultureIgnoreCase).Replace("Controller", "", StringComparison.CurrentCultureIgnoreCase);
                var route = item.GetCustomAttribute<RouteAttribute>();
                var url = route.Template.Replace("{Controller}", controllerName, StringComparison.CurrentCultureIgnoreCase).Replace("[Controller]", controllerName, StringComparison.CurrentCultureIgnoreCase);
                var methods = item.GetMethods().Where(p => p.GetCustomAttribute<RouteAttribute>() != null &&
                  (isAuth == 1 ? p.GetCustomAttribute<AllowAnonymousAttribute>() == null : true));
                foreach (var method in methods)
                {
                    var attr = method.GetCustomAttribute<RouteAttribute>();
                    list.Add(new
                    {
                        Api = $"/{url}/{attr.Template}",
                        Explain = attr.Name
                    });
                }
            }

            return await Result.OKAsync(list);

        }
    }
}
